package com.cyanite.playlv2.util;

import java.util.Calendar;
import java.util.Date;

/**
 * Initializes a few Date objects to act as boundaries for sorting checkin lists
 * by the following time categories:
 *
 * <ul>
 *   <li>Within the last three hours.</li>
 *   <li>Today</li>
 *   <li>Yesterday</li>
 * </ul>
 *
 * Create an instance of this class, then call one of the three getBoundary() methods
 * and compare against a Date object to see if it falls before or after.
 */
public class CheckinTimestampSort {


    private static final int IDX_RECENT = 0;
    private static final int IDX_TODAY = 1;
    private static final int IDX_YESTERDAY = 2;
    private static final int IDX_ACCURATE_RECENT = 3;

    private Date[] mBoundaries;


    public CheckinTimestampSort() {
        mBoundaries = getDateObjects();
    }

    public Date getBoundaryRecent() {
        return mBoundaries[IDX_RECENT];
    }

    public Date getBoundaryToday() {
        return mBoundaries[IDX_TODAY];
    }

    public Date getBoundaryYesterday() {
        return mBoundaries[IDX_YESTERDAY];
    }

    public Date getBoundaryAccurateRecent() {
        return mBoundaries[IDX_ACCURATE_RECENT];
    }

    private static Date[] getDateObjects() {

        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());

        Date dateAccurateRecent = cal.getTime();

        // Three hours ago or newer.
        cal.add(Calendar.HOUR, -3);
        Date dateRecent = cal.getTime();

        // Today.
        cal.clear(Calendar.HOUR_OF_DAY);
        cal.clear(Calendar.HOUR);
        cal.clear(Calendar.MINUTE);
        cal.clear(Calendar.SECOND);
        Date dateToday = cal.getTime();

        // Yesterday.
        cal.add(Calendar.DAY_OF_MONTH, -1);
        Date dateYesterday = cal.getTime();
        return new Date[] { dateRecent, dateToday, dateYesterday, dateAccurateRecent };
    }
}
